<template>
  <div>
    <!-- 新增修改表单 -->
    <el-dialog
      width="50%"
      :title="textMap[dialogStatus]"
      :visible.sync="DialogFormVisibleCom"
      append-to-body
    >
      <div class="myfrom1002">


        <el-form ref="dataForm" :rules="rules" :model="formSubmitDataCom" label-position="right" label-width="110px"
                 style="width: 90%; margin:10px auto;">

          <el-row :gutter="20">
            <el-col :span="24">
              <!-- 期限编码 -->
              <el-form-item label="基础曲线" prop="bs_cv_code">
                <el-input
                  v-model="formSubmitDataCom.bs_cv_code"
                  :disabled="true"
                />
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <!-- 生效日期 -->
              <el-form-item label="生效日期" prop="s_date">
                <el-date-picker
                  v-model="formSubmitDataCom.s_date"
                  type="date"
                  placeholder="Please pick a date"
                  value-format="yyyy-MM-dd"
                  :disabled="true"
                />
              </el-form-item>

            </el-col>

            <el-col :span="12">
              <!-- 失效日期 -->
              <el-form-item label="失效日期" prop="e_date">
                <el-date-picker
                  v-model="formSubmitDataCom.e_date"
                  type="date"
                  placeholder="Please pick a date"
                  value-format="yyyy-MM-dd"
                  :disabled="true"
                />
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="原曲线类型" prop="m_cv_type">
                <el-select
                  v-model="formSubmitDataCom.m_cv_type"
                  class="filter-item"
                  placeholder="Please select"
                  @change="mCvTypeChange"
                >
                  <el-option v-for="item in bCCurveType" :key="item.code" :label="item.name"
                             :value="item.code"/>
                </el-select>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="原曲线" prop="m_cv_code">
                <el-select
                  v-model="formSubmitDataCom.m_cv_code"
                  class="filter-item"
                  placeholder="Please select"
                  @change="mCvCodeChange"
                >
                  <el-option v-for="item in bCCurve" :key="item.cv_code" :label="item.cv_name"
                             :value="item.cv_code"/>
                </el-select>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <!-- 期限编码 -->
              <el-form-item label="原曲线期限点" prop="m_term_code">
                <el-select
                  v-model="formSubmitDataCom.m_term_code"
                  class="filter-item"
                  placeholder="Please select"
                >
                  <el-option v-for="item in mTermCodeOptions" :key="item.term_code" :label="item.term_name"
                             :value="item.term_code"/>
                </el-select>

              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="相减曲线类型" prop="s_cv_type">
                <el-select
                  v-model="formSubmitDataCom.s_cv_type"
                  class="filter-item"
                  placeholder="Please select"
                  @change="sCvTypeChange"
                >
                  <el-option v-for="item in sCvTypeOptions" :key="item.code" :label="item.name"
                             :value="item.code"/>
                </el-select>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="相减曲线" prop="s_cv_code">
                <el-select
                  v-model="formSubmitDataCom.s_cv_code"
                  class="filter-item"
                  placeholder="Please select"
                  @change="sTermCodeChange"
                >
                  <el-option v-for="item in sCvCodeOptions" :key="item.cv_code" :label="item.cv_name"
                             :value="item.cv_code"/>
                </el-select>
              </el-form-item>
            </el-col>


            <el-col :span="12">
              <!-- 期限编码 -->
              <el-form-item label="相减曲线期限点" prop="s_term_code">
                <el-select
                  v-model="formSubmitDataCom.s_term_code"
                  class="filter-item"
                  placeholder="Please select"
                >
                  <el-option v-for="item in sTermCodeOptions" :key="item.term_code" :label="item.term_name"
                             :value="item.term_code"/>
                </el-select>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="调整类型" prop="adj_type">
                <el-select v-model="formSubmitDataCom.adj_type" class="filter-item" placeholder="Please select"
                >
                  <el-option v-for="item in adjTypeOptions" :key="item.code" :label="item.name"
                             :value="item.code"/>
                </el-select>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <!-- 期限编码 -->
              <el-form-item label="值" prop="adj_value">
                <el-input
                  v-model="formSubmitDataCom.adj_value"
                />
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="调整单位" prop="adj_cp">
                <el-select v-model="formSubmitDataCom.adj_cp" class="filter-item" placeholder="Please select"
                >
                  <el-option v-for="item in adjCpOptions" :key="item.code" :label="item.name"
                             :value="item.code"/>
                </el-select>
              </el-form-item>
            </el-col>

            <el-col :span="24">
              <!-- 备注 -->
              <el-form-item label="备注：" prop="remark">
                <el-input type="textarea" v-model="formSubmitDataCom.remark" placeholder="Please select"></el-input>
              </el-form-item>
            </el-col>
          </el-row>


        </el-form>
      </div>
      <div slot="footer" class="dialog-footer" style="text-align:center">
        <el-button @click="resetFormData">
          重置
        </el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">
          提交
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import XEUtils from 'xe-utils'

import SubmitFormAItem from "./SubmitFormAItem"

export default {
  name: 'submitForm',
  components: {
    SubmitFormAItem
  },
  props: {
    dialogFormVisible: {
      type: Boolean,
      default: false
    },
    dialogStatus: {
      type: String,
    },
    // tableColumn: {
    //     type: Array,
    //     default() {
    //         return []
    //     }
    // },
    formSubmitData: {
      type: Object,
      default: function () {
        return {
          // id: undefined,
          // cv_name: '', //曲线名称
          // curr_name: '', //币种
          // s_date: '', //生效日期
          // cv_type: '',
          // e_date: '', //失效日期
          // tp_id: '', //期限方案id
          // remark: ''
        }
      }
    },
    // 曲线期限列表
    curveDeadlineList: {
      type: Array
    },
    // 曲线期限设置列表
    curveDeadlineSetList: {
      type: Array
    },
    // 原曲线类型
    bCCurveType: {
      type: Array
    },
    // 原曲线
    bCCurve: {
      type: Array
    },
    // 调整类型选项
    adjTypeOptions: {
      type: Array
    },
    // 调整值选项
    adjCpOptions: {
      type: Array
    },
    // 相减类型
    sCvTypeOptions: {
      type: Array
    },
    // 相减曲线
    sCvCodeOptions: {
      type: Array
    },
    // 原曲线期限点
    mTermCodeOptions: {
      type: Array
    },
    // 相减曲线期限点
    sTermCodeOptions: {
      type: Array
    },

  },
  // 定义属性
  data() {
    /* 生效日期验证 */
    var validateDateRangeSdate = (rule, value, callback) => {
      if (XEUtils.isEmpty(value)) {
        callback(new Error('生效日期必填!'));
      } else if ((!XEUtils.isEmpty(this.formSubmitDataCom.e_date)) && (value > this.formSubmitDataCom.e_date)) {
        callback(new Error('生效日期必须小于失效日期!'));
      } else {
        callback();
      }
    };
    /* 失效日期验证 */
    var validateDateRangeEdate = (rule, value, callback) => {
      if (XEUtils.isEmpty(value)) {
        callback(new Error('失效日期必填!'));
      } else if ((!XEUtils.isEmpty(this.formSubmitDataCom.s_date)) && (value < this.formSubmitDataCom.s_date)) {
        callback(new Error('失效日期必须大于生效日期!'));
      } else {
        callback();
      }
    };

    return {
      textMap: {
        update: '修改',
        create: '新增'
      },
      rules: {
        tp_id: [{required: true, message: '期限方案必填', trigger: 'change'}],
        s_date: [{validator: validateDateRangeSdate, trigger: 'change'}],
        e_date: [{validator: validateDateRangeEdate, trigger: 'change'}],
        // type: [{ required: true, message: 'type is required', trigger: 'change' }],
        // timestamp: [{ type: 'date', required: true, message: 'timestamp is required', trigger: 'change' }],
        // title: [{ required: true, message: 'title is required', trigger: 'blur' }]
      },
      // 表单用到的值
      // calendarTypeOptions,
      // statusOptions: ['published', 'draft', 'deleted'],
    }
  },
  // 计算属性，会监听依赖属性值随之变化
  computed: {
    DialogFormVisibleCom: {
      get() {
        return this.dialogFormVisible
      },
      set(val) {
        this.$emit('update:dialogFormVisible', val)
      }
    },
    formSubmitDataCom: {
      get() {
        return this.formSubmitData
      },
      set(val) {
        this.$emit('update:formSubmitData', val)
      }
    }
  },
  // 监控data中的数据变化
  watch: {},
  // 方法集合
  methods: {
    mCvTypeChange(val) {
      console.log('OOOOOOOOOOOOOPPPPPPPJJJ', val);
      this.$emit('mCvTypeChangeEmit', val)
    },
    sCvTypeChange(val) {
      console.log('yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy');
      this.$emit('sCvTypeChangeEmit', val)
    },
    mCvCodeChange(val) {
      this.$emit('mCvCodeChangeEmit', val)
    },
    sTermCodeChange(val) {
      this.$emit('sTermCodeChangeEmit', val)
    },
    resetFormData() {
      console.log("IIIIIIIIIIUUUUUUUUUUUUUUUUU");
      let exclude = []
      if (this.dialogStatus === 'create') {
        exclude = [
          // 'cv_name',
          // 'curr_name',
          // 'e_date'
        ]
      }
      console.log('this.dialogStatusthis.dialogStatus', this.dialogStatus);
      if (this.dialogStatus === 'update') {
        exclude = [
          // 'cv_name',
          // 'curr_name',
          // 'tp_id'
        ]
      }
      console.log('exclude', exclude);

      this.$emit('resetFormDataEmit', {
        exclude
      })

    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$emit('handleFromCreateDataEmit')
          // this.temp.id = parseInt(Math.random() * 100) + 1024 // mock a id
          // this.temp.author = 'vue-element-admin'
          // createArticle(this.temp).then(() => {
          //   this.list.unshift(this.temp)
          //   this.dialogFormVisible = false
          //   this.$notify({
          //     title: 'Success',
          //     message: 'Created Successfully',
          //     type: 'success',
          //     duration: 2000
          //   })
          // })
        }
      })
    },

    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$delete(this.formSubmitData, "_XID");
          this.$delete(this.formSubmitData, "lp_name");
          const tempData = Object.assign({}, this.formSubmitData)
          this.$emit('handleFromUpdateDataEmit')
          // tempData.timestamp = +new Date(tempData.timestamp) // change Thu Nov 30 2017 16:41:05 GMT+0800 (CST) to 1512031311464
          // updateArticle(tempData).then(() => {
          //   const index = this.list.findIndex(v => v.id === this.temp.id)
          //   this.list.splice(index, 1, this.temp)
          //   this.dialogFormVisible = false
          //   this.$notify({
          //     title: 'Success',
          //     message: 'Update Successfully',
          //     type: 'success',
          //     duration: 2000
          //   })
          // })
        }
      })
    },

  },
  // 生命周期 - 创建完成（可以访问当前this实例）
  created() {

  },
  // 生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {

  },
  beforeCreate() {
  }, // 生命周期 - 创建之前
  beforeMount() {
  }, // 生命周期 - 挂载之前
  beforeUpdate() {
  }, // 生命周期 - 更新之前
  updated() {
  }, // 生命周期 - 更新之后
  beforeDestroy() {
  }, // 生命周期 - 销毁之前
  destroyed() {
  }, // 生命周期 - 销毁完成
  activated() {
  }, // 如果页面有keep-alive缓存功能，这个函数会触发
}
</script>

<style lang='scss' scoped>
.myfrom1002 {
  ::v-deep {
    .el-col-12 {
      height: 58px;
    }

  }
}


</style>